const { add, find, update, del,addAll } = require("../../server");
const vacation = require("../../model/vacation");
const vacation1 = require("../../model/vacation1");

//查询全部数据 /admin/vacation/vacationsAll
const getCtr = async (req, res, next) => {
  try {
    let data = await find(vacation);
    res.send({
      code: 200,
      data,
    });
  } catch (err) {
    console.log(err);
    res.send({
      code: 400,
      message: "数据查询失败",
    });
  }
};

//模糊查询
const getCtr1 = async (req, res, next) => {
  let tiao = req.query;
  try {
    let data = await find(vacation, {
      nickname: tiao.nickname,
    });
    let data1 = await find(vacation, {
      position: tiao.position,
    });
    let data2 = await find(vacation, {
      nickname: tiao.nickname,
      position: tiao.position,
    });
    if (tiao.nickname == "" && tiao.position != "") {
      res.send({
        code: 200,
        data: data1,
      });
    } else {
      if (tiao.nickname != "" && tiao.position == "") {
        res.send({
          code: 200,
          data: data,
        });
      } else {
        res.send({
          code: 200,
          data: data2,
        });
      }
    }
  } catch (err) {
    console.log(err);
    res.send({
      code: 400,
      message: "数据查询失败",
    });
  }
};

//增加  /admin/vacation/vacations
const postCtr = async (req, res, next) => {
  let data = req.body;
  try {
    await add(vacation, data);
    res.send({
      code: 200,
      message: "增加成功",
    });
  } catch (error) {
    res.send({
      code: 400,
      message: "增加失败",
    });
  }
};

// 文件导入
const postCtr1 = async (req, res, next) => {
  let data = req.body;
  try {
    await addAll(vacation, data);
    res.send({
      code: 200,
      message: "数据插入成功",
    });
  } catch (error) {
    console.log(error);
    res.send({
      code: 400,
      message: "数据插入失败",
    });
  }
};

// 信息修改
const putCtr = async (req, res, next) => {
  let data = req.body;
  try {
    let bob = await update(vacation, data.newTableDate, {
      id: data.id,
    });
    if (bob == 0) {
      res.send({
        code: 400,
        message: "未进行修改操作",
      });
      return;
    }
    res.send({
      code: 200,
      message: "修改成功",
    });
  } catch (error) {
    console.log(error);
    res.send({
      code: 400,
      message: "修改失败，请联系超级管理员",
    });
  }
};

// 接收加班申请的传值
const putCtr1 = async (req, res, next) => {
  let data = req.body;
  try {
    let name = await find(vacation, { nickname: data.nickname });
    if (name.length == 0) {
      res.send({
        code: 400,
        message: "没有找到对应的名字",
      });
      return;
    }
    if (data.overtime_duration <= 0) {
      res.send({
        code: 400,
        message: "没有调休时间",
      });
    } else {
      let hour =
        Number(name[0].compensatory_leave) + Number(data.overtime_duration);
      let hours = await update(
        vacation,
        { compensatory_leave: hour },
        {
          nickname: data.nickname,
        }
      );
      res.send({
        code: 200,
        message: "现在调休时间为" + data.overtime_duration,
      });
    }
  } catch (error) {
    console.log(error);
  }
};

// 接收传值
const putCtr2 = async (req, res, next) => {
  let data = req.body;
  console.log(data);
  try {
    let name = await find(vacation, { nickname: data.nickname });
    if (name.length == 0) {
      res.send({
        code: 400,
        message: "没有找到对应的名字",
      });
      return;
    }
    if (name[0].compensatory_leave <= 0) {
      res.send({
        code: 400,
        message: "没有调休时间",
      });
    } else {
      let hour =
        Number(name[0].compensatory_leave) - Number(data.duration_hours);
      console.log(hour);
      let hours = await update(
        vacation,
        { compensatory_leave: hour },
        {
          nickname: data.nickname,
        }
      );
      res.send({
        code: 200,
        message: "现在可调休时间是" + data.duration_hours,
      });
    }
  } catch (error) {
    console.log(error);
  }
};

// 删除数据
const delCtr = async (req, res, next) => {
  try {
    let bob = await del(vacation, {
      id: req.body.id,
    });
    if (bob == 0) {
      res.send({
        code: 400,
        message: "删除失败，请联系程序员",
      });
      return;
    }
    res.send({
      code: 200,
      message: "删除成功",
    });
  } catch (error) {
    console.log(error);
    res.send({
      code: 400,
      message: "删除失败，请联系程序员",
    });
  }
};

//----------------回收站-------------------
//查询全部数据 
const getRecycle = async (req, res, next) => {
  try {
    let data = await find(vacation1);
    res.send({
      code: 200,
      data,
    });
  } catch (err) {
    console.log(err);
    res.send({
      code: 400,
      message: "数据查询失败",
    });
  }
};
//模糊查询
const getRecycles = async (req, res, next) => {
  let tiao = req.query;
  try {
    let data = await find(vacation1, {
      nickname: tiao.nickname,
    });
    let data1 = await find(vacation1, {
      position: tiao.position,
    });
    let data2 = await find(vacation1, {
      nickname: tiao.nickname,
      position: tiao.position,
    });
    if (tiao.nickname == "" && tiao.position != "") {
      res.send({
        code: 200,
        data: data1,
      });
    } else {
      if (tiao.nickname != "" && tiao.position == "") {
        res.send({
          code: 200,
          data: data,
        });
      } else {
        res.send({
          code: 200,
          data: data2,
        });
      }
    }
  } catch (err) {
    console.log(err);
    res.send({
      code: 400,
      message: "数据查询失败",
    });
  }
};
//增加  
const postRecycle = async (req, res, next) => {
  let data = req.body;
  try {
    await add(vacation1, data);
    res.send({
      code: 200,
      message: "增加成功",
    });
  } catch (error) {
    res.send({
      code: 400,
      message: "增加失败",
    });
  }
};
// 删除数据
const delRecycle = async (req, res, next) => {
  try {
    let bob = await del(vacation1, {
      id: req.body.id,
    });
    if (bob == 0) {
      res.send({
        code: 400,
        message: "删除失败，请联系程序员",
      });
      return;
    }
    res.send({
      code: 200,
      message: "删除成功",
    });
  } catch (error) {
    console.log(error);
    res.send({
      code: 400,
      message: "删除失败，请联系程序员",
    });
  }
};
// 恢复数据
const delRecycles = async (req, res, next) => {
  try {
    let bob = await del(vacation1, {
      id: req.body.id,
    });
    if (bob == 0) {
      res.send({
        code: 400,
        message: "恢复失败，请联系程序员",
      });
      return;
    }
    res.send({
      code: 200,
      message: "恢复成功",
    });
  } catch (error) {
    console.log(error);
    res.send({
      code: 400,
      message: "恢复失败，请联系程序员",
    });
  }
};
module.exports = {
  getCtr,
  postCtr,
  putCtr,
  delCtr,
  getCtr1,
  putCtr1,
  putCtr2,getRecycle,getRecycles,postRecycle,delRecycle,delRecycles,
  postCtr1
};
